//
//  RunLoop.hpp
//  Simulation
//
//  Created by Christopher Kemsley on 2/24/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//

#ifndef Simulation_RunLoop_hpp
#define Simulation_RunLoop_hpp

#include <set>
#include "Types.hpp"
#include "System.hpp"
#include "AutoreleasePool.hpp"



// This is to help with auto-indentation
NamespaceBegin





class	RunLoop ;
class	RunLoopSource ;










/*************** RunLoop ***************/

class	RunLoop : private SystemController
{
public:
	
	
	// Getting the current run loop
	static RunLoop&	currentLoop () ;
	
	
	// Running the loop
	void	run	() ;
	
	
	// Autorelease pool
	AutoreleasePool&	currentPool () ;
	
	
	
	
	// Internal structure
private:
	typedef std::set<RunLoopSource*>	Sources ;
	
	Sources			mSources ;
	AutoreleasePool	mPool ;
	
	RunLoop () ;
	~RunLoop () ;
	
	virtual Interval const	minimumTimeInterval () const ;
	
	friend class RunLoopSource ;
};











/********** RunLoopSource **********/

class	RunLoopSource
{
private:
	friend class RunLoop ;
	
	// Implement this
	virtual void			fire () = 0 ;
	virtual Interval const	fireDate () ; // override if you can get this information
	
protected:
	// Use these to register/unregister with the loop
	void	performRegistration () ;
	void	performUnregistration () ;
} ;





NamespaceEnd
#endif
